package com.newstore.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Classe qui permet de créer l'ensemble des tables et des colonnes
 * @author Guillaume
 * @version 1.0
 */
public class DatabaseNewstore extends SQLiteOpenHelper
{
	
	// Constantes du produit
	public static final String COL_REFERENCE = "reference";
	public static final String COL_LOCATION = "location";
	public static final String COL_NAME = "name";
	public static final String COL_CATEGORY = "category";
	public static final String COL_BARCODE = "barcode";
	public static final String COL_STOCK = "stock";
	public static final String TABLE_PRODUCT = "tableProduct";
	
	private static final String QUERY_PRODUCT = "CREATE TABLE " + TABLE_PRODUCT + "( " +
			"id INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_NAME + " TEXT NOT NULL, " + COL_REFERENCE + " TEXT NOT NULL, " +
			COL_LOCATION + " TEXT NOT NULL, " + COL_CATEGORY + " TEXT NOT NULL, " + COL_BARCODE + " INTEGER, " + COL_STOCK + " INTEGER);";
	
	// Constantes de commande
	public static final String COL_STATUS = "status";
	public static final String COL_ORDER_REFERENCE = "referenceOrder";
	public static final String COL_ORDER_BARCODE = "barcodeOrder";
	public static final String COL_DELIVERY = "deliveryOrder";
	public static final String TABLE_ORDER = "tableOrder";
	
	private static final String QUERY_ORDER = "CREATE TABLE " + TABLE_ORDER + "( " +
			"id INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_ORDER_REFERENCE + " TEXT NOT NULL, " + COL_STATUS + " TEXT NOT NULL, " + COL_ORDER_BARCODE + " INTEGER, " + COL_DELIVERY + " INTEGER);";
	
	// Constantes de etreDans
	public static final String COL_COMMAND = "idCommand";
	public static final String COL_PRODUCT = "idProduct";
	public static final String COL_NUMBERPRODUCT = "numberProduct";
	public static final String TABLE_TOBEIN = "tableProductCommand";
	
	private static final String QUERY_TOBEIN = "CREATE TABLE " + TABLE_TOBEIN + "( " +
			COL_COMMAND + " INTEGER, " + COL_PRODUCT + " INTEGER, " + COL_NUMBERPRODUCT + " INTEGER);";
	
	// constantes de destinataire
	public static final String COL_ADDRESS = "address";
	public static final String COL_NAME_DELIVERY = "name";
	public static final String COL_REFERENCE_DELIVERY = "reference";
	public static final String TABLE_DELIVERY = "tableDelivery";
	
	private static final String QUERY_DELIVERY = "CREATE TABLE " + TABLE_DELIVERY + "( " +
			"id INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_NAME_DELIVERY + " TEXT NOT NULL, " + COL_REFERENCE_DELIVERY + " TEXT NOT NULL, " +
			COL_ADDRESS + " TEXT NOT NULL);";

	// constantes de user
	public static final String COL_LOGIN = "login";
	public static final String COL_PWD = "password";
	public static final String COL_TYPE = "type";
	public static final String TABLE_USER = "tableUser";
	
	private static final String QUERY_USER = "CREATE TABLE " + TABLE_USER + "( " +
			"id INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_LOGIN + " TEXT NOT NULL, " + COL_PWD + " TEXT NOT NULL, " +
			COL_TYPE + " TEXT NOT NULL);";
	
	// constantes de barcode
	public static final String COL_PREFIX_ELEMENT = "prefixElement";
	public static final String COL_SECOND_ELEMENT = "secondElement";
	public static final String COL_THIRD_ELEMENT = "thirdElement";
	public static final String COL_FOURTH_ELEMENT = "fourthElement";
	public static final String TABLE_BARCODE = "tableBarcode";
	
	private static final String QUERY_BARCODE = "CREATE TABLE " + TABLE_BARCODE + "( " +
			"id INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_PREFIX_ELEMENT + " INTEGER, " + COL_SECOND_ELEMENT + " INTEGER, " +
			COL_THIRD_ELEMENT + " INTEGER, " + COL_FOURTH_ELEMENT + " INTEGER);";
	
	/**
	 * Constructeur qui permet d'instancier la création des tables
	 * @param context
	 * @param name
	 * @param factory
	 * @param version
	 */
	public DatabaseNewstore(Context context, String name, CursorFactory factory, int version)
	{
		super(context, name, factory, version);
	}
	
	/**
	 *  Crée les tables lors de la création de la classe
	 *  @param database Base de données où créer les tables
	 */
	@Override
	public void onCreate(SQLiteDatabase database)
	{
		database.execSQL(QUERY_PRODUCT);
		database.execSQL(QUERY_ORDER);
		database.execSQL(QUERY_TOBEIN);
		database.execSQL(QUERY_DELIVERY);
		database.execSQL(QUERY_USER);
		database.execSQL(QUERY_BARCODE);
	}

	/** 
	 * Lors d'un changement de version de la base données, l'ensemble des tables sont effacées puis recréées
	 * @param database : base de données où effectuer l'upgrade
	 */ 
	@Override
	public void onUpgrade(SQLiteDatabase database, 
			int oldVersion, int newVersion)
	{
		database.execSQL("DROP TABLE " + TABLE_BARCODE + ";");
		database.execSQL("DROP TABLE " + TABLE_PRODUCT + ";");
		database.execSQL("DROP TABLE " + TABLE_TOBEIN + ";");
		database.execSQL("DROP TABLE " + TABLE_USER + ";");
		database.execSQL("DROP TABLE " + TABLE_DELIVERY + ";");
		database.execSQL("DROP TABLE " + TABLE_ORDER + ";");
		onCreate(database);
	}

}
